package utils;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;


/**
 * Some util function for selecting File, String, integer and others.
 * 
 * @author Hugo
 * 
 */
public class ChooserUtils
{
    public final static String jpeg      = "jpeg";
    public final static String jpg       = "jpg";
    public final static String png       = "png";
    public final static String cancelMsg = "SELECTIONCANCEL";

    /**
     * Get the extension of a file.
     */
    public static String getExtension(File f)
    {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 && i < s.length() - 1)
        {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }

    /**
     * Select an Image File
     * 
     * @return String path : the path of the file
     */
    public static String selectImageFile()
    {
        JFileChooser imgFileChooser = new JFileChooser();
        imgFileChooser.setAcceptAllFileFilterUsed(false);
        imgFileChooser.addChoosableFileFilter(new FileFilter()
        {
            @Override
            public String getDescription()
            {
                return "Only (" + jpg + "," + jpeg + "," + png + ")";
            }

            @Override
            public boolean accept(File myFile)
            {
                if (myFile.isDirectory())
                {
                    return true;
                }

                String extension = getExtension(myFile);
                if (extension != null)
                {
                    if (extension.equals(jpg) || extension.equals(jpeg)
                            || extension.equals(png))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }

                return false;
            }
        });

        if (imgFileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        {
            return imgFileChooser.getSelectedFile().getAbsolutePath();
        }
        else
        {
            return cancelMsg;
        }
    }

    /**
     * Ask the user to enter an integer
     * 
     * @param question
     *            (String) : your question
     * @return intValue (int) : his answer
     */
    public static int selectInt(String question)
    {
        String value = JOptionPane.showInputDialog(null, question);
        int intValue = Integer.parseInt(value);
        return intValue;
    }

    /**
     * Ask the user to enter a String
     * 
     * @param question
     *            (String) : your question
     * @return value (String) : his answer
     */
    public static String selectString(String question)
    {
        String value = JOptionPane.showInputDialog(null, question);
        return value;
    }
}
